<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item :label="$t('system.msgNotice.noticeTitle')" prop="noticeTitle">
        <el-input
          v-model="queryParams.noticeTitle"
          :placeholder="$t('constant.input')+$t('system.msgNotice.noticeTitle')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('system.msgNotice.operator')" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          :placeholder="$t('constant.input')+$t('system.msgNotice.operator')"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('tagsView.search') }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('tagsView.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
        >{{ $t('tagsView.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:notice:edit']"
        >{{ $t('tagsView.edit') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:notice:remove']"
        >{{ $t('tagsView.delete') }}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('system.msgNotice.serialNumber')" align="center" prop="noticeId" width="120"/>
      <el-table-column
        :label="$t('system.msgNotice.noticeTitle')"
        align="center"
        prop="noticeTitle"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('system.msgNotice.noticeState')"
        align="center"
        prop="status"
        :formatter="statusFormat"
        width="100"
      />
      <el-table-column :label="$t('system.msgNotice.operator')" align="center" prop="createBy" width="100"/>
      <el-table-column :label="$t('system.public.creationTime')" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('tagsView.operation')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:notice:edit']"
          >{{ $t('tagsView.edit') }}
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:notice:remove']"
          >{{ $t('tagsView.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('system.msgNotice.noticeTitle')" prop="noticeTitle">
              <el-input v-model="form.noticeTitle"
                        :placeholder="$t('constant.input')+$t('system.msgNotice.noticeTitle')"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('system.msgNotice.noticeState')">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('system.msgNotice.noticeDesc')">
              <el-input v-model="form.noticeDesc"
                        :placeholder="$t('constant.input')+$t('system.msgNotice.noticeDesc')"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('system.msgNotice.noticeContent')">
              <editor v-model="form.noticeContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('tagsView.ok') }}</el-button>
        <el-button @click="cancel">{{ $t('tagsView.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { msgListNotice, getNotice, delNotice, addNotice, updateNotice, exportNotice } from '@/api/system/notice'
import Editor from '@/components/Editor'

export default {
  name: 'Notice',
  components: {
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      statusOptions: [],
      // 状态数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeTitle: [
          {
            required: true,
            message: this.$t('system.msgNotice.noticeTitle') + this.$t('constant.noEmpty'),
            trigger: 'blur'
          },
          { min: 1, max: 200, message: this.$t('system.notice.noticeTitleLength'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_notice_status').then(response => {
      this.statusOptions = response.data
    })
    this.getDicts('sys_notice_type').then(response => {
      this.typeOptions = response.data
    })
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true
      msgListNotice(this.queryParams).then(response => {
        this.noticeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.noticeType)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        noticeDesc: undefined,
        status: '0'
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('system.msgNotice.dialogMsg.title_add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = this.$t('system.msgNotice.dialogMsg.title_edit')
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.noticeType = '0'
          if (this.form.noticeId != undefined) {
            updateNotice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('system.public.editSucceeded'))
                this.open = false
                this.getList()
              }
            })
          } else {
            addNotice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('system.public.addSucceeded'))
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids
      this.$confirm(this.$t('system.msgNotice.dialogMsg.delete_tips1') + noticeIds + this.$t('system.msgNotice.dialogMsg.delete_tips2'), this.$t('tagsView.warning'), {
        confirmButtonText: this.$t('tagsView.ok'),
        cancelButtonText: this.$t('tagsView.cancel'),
        type: 'warning'
      }).then(function() {
        return delNotice(noticeIds)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('system.public.deletionSucceeded'))
      }).catch(function() {
      })
    }
  }
}
</script>
